﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using HistoryToMail;

namespace QIP
{
    public partial class TestApplicationMainForm : Form
    {
        private HistoryToMailEngine historyEngine = new HistoryToMailEngine();

        public TestApplicationMainForm()
        {
            InitializeComponent();
            historyEngine.SettingsChanged += new PluginSettingWhereChanged(SettingsWhereChanged);
            historyEngine.LogMsgAvailable += new LogMessage(messageToSendReady);
            try
            {
                historyEngine.loadSettingsFromString(Properties.Settings.Default.SMTPSettings);
            }
            catch (Exception exception)
            {
            }
        }

        private void pluginSetting_Click(object sender, EventArgs e)
        {
            historyEngine.showSettingsDialog();
        }

        private void SettingsWhereChanged(String settings)
        {
            try
            {
                Properties.Settings.Default.SMTPSettings = settings;
                Properties.Settings.Default.Save();
                historyEngine.AddLog(settings);
            }
            catch (Exception exception)
            {
            }
        }

        private void protocolCombo_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void addButton_Click(object sender, EventArgs e)
        {
            historyEngine.addMessage(protocolCombo.Text, peopleCombo.Text, "me", MessageType.inp, textBox.Text, DateTime.Now);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            historyEngine.addMessage(protocolCombo.Text, "me", peopleCombo.Text, MessageType.outp, textBox.Text, DateTime.Now);
        }


        private delegate void SetTextCallback(String data);

        private void messageToSendReady(String msg)
        {
            SetTextCallback d = new SetTextCallback(setText);
            this.Invoke(d, new object[] { msg });

        }

        private void setText(String msg)
        {
            textToSend.Text += msg + "\r\n";
        }

        private void TestApplicationMainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }

        private void TestApplicationMainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            historyEngine.SettingsChanged -= new PluginSettingWhereChanged(SettingsWhereChanged);
            historyEngine.LogMsgAvailable -= new LogMessage(messageToSendReady);
            historyEngine.Stop();
            historyEngine = null;
        }
    }
}
